package com.yupi.springbootinit.share.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.springbootinit.share.utils.common.IpResponseBodyResult;
import com.yupi.springbootinit.share.utils.common.ShareChicken;
import com.yupi.springbootinit.share.utils.common.WeatherData;
import com.yupi.springbootinit.share.utils.common.WeatherResponseBodyResult;
import com.yupi.springbootinit.share.utils.mapper.ShareChickenMapper;
import com.yupi.springbootinit.share.utils.wallpaper.WallpaperEnum;
import com.yupi.springbootinit.share.utils.wallpaper.WallpaperResponseBodyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 此接口为通用接口
 */
@RestController
@RequestMapping("/share/common")
public class CommonController {

    @Autowired
    private ShareChickenMapper chickenMapper;


    /**
     * 解析IP地址
     * @param ip IP4地址
     * @return IpResponseBodyResult
     */
    @PostMapping("/ip")
    public IpResponseBodyResult searchIp(@RequestParam("ip") String ip) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ip", ip);
        IpResponseBodyResult parse = JSONUtil.toBean(
                HttpUtil.get("https://tea.qingnian8.com/tools/placename", map),
                IpResponseBodyResult.class);
        parse.setAuthor("");
        return parse;
    }

    /**
     * 获取城市天气
     * @param weather 城市名
     * @return WeatherResponseBodyResult
     */
    @PostMapping("/weather")
    public WeatherResponseBodyResult searchWeather(@RequestParam("weather") String weather) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("weather", weather);
        WeatherResponseBodyResult parse = JSONUtil.toBean(
                HttpUtil.get("https://tea.qingnian8.com/tools/weather", map),
                WeatherResponseBodyResult.class);
        parse.setAuthor("");
        return parse;
    }

    /**
     * 每日励志鸡汤
     * @return text
     */
    @GetMapping("/chicken")
    public String everyDayChicken() {
        ShareChicken chicken = chickenMapper.getChicken();
        return chicken.getText();
    }


}
